using System;
using System.Collections.Generic;
using UnityEngine;

public class BMTheme : ScriptableObject
{
	public static BMTheme Instance
	{
		get
		{
			if (BMTheme.instance == null)
			{
				BMTheme.instance = (Resources.Load("BMThemeSettings") as BMTheme);
				if (BMTheme.instance == null)
				{
					BMTheme.instance = ScriptableObject.CreateInstance<BMTheme>();
				}
			}
			return BMTheme.instance;
		}
	}

	public string ThemeName
	{
		get
		{
			return this.currentThemeName;
		}
		set
		{
			if (this.currentThemeName == value)
			{
				return;
			}
			this.currentThemeName = value;
			this.Change(this.currentThemeName);
		}
	}

	private void OnEnable()
	{
		UnityEngine.Object.DontDestroyOnLoad(this);
		if (this.currentThemeName == null && this.themeInfos != null && this.themeInfos.Length > 0)
		{
			this.currentThemeName = this.themeInfos[0].themeName;
		}
	}

	public void Register(BMThemeBase themeItem)
	{
		if (!this.themeItems.Contains(themeItem))
		{
			this.themeItems.Add(themeItem);
		}
		if (!string.IsNullOrEmpty(this.ThemeName))
		{
			themeItem.Change(this.ThemeName);
		}
	}

	public void Unregister(BMThemeBase themeItem)
	{
		this.themeItems.Remove(themeItem);
	}

	public bool ContainsTheme(string themeName)
	{
		bool result = false;
		if (this.themeInfos == null)
		{
			return result;
		}
		foreach (ThemeInfo themeInfo in this.themeInfos)
		{
			if (themeInfo != null)
			{
				if (themeInfo.themeName == themeName)
				{
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public tk2dSpriteCollectionData GetCollection(string themeName, tk2dSpriteCollectionData currentCollection)
	{
		tk2dSpriteCollectionData result = currentCollection;
		if (this.themeInfos == null)
		{
			UnityEngine.Debug.LogWarning("Can't find theme item.");
			return result;
		}
		int num = -1;
		foreach (ThemeInfo themeInfo in this.themeInfos)
		{
			for (int j = 0; j < themeInfo.collections.Length; j++)
			{
				if (currentCollection == themeInfo.collections[j])
				{
					num = j;
					break;
				}
			}
			if (num != -1)
			{
				break;
			}
		}
		if (num == -1)
		{
			UnityEngine.Debug.LogWarning("Can't find pair theme collection.");
		}
		else
		{
			foreach (ThemeInfo themeInfo2 in this.themeInfos)
			{
				if (themeInfo2.themeName == themeName)
				{
					result = themeInfo2.collections[num];
					break;
				}
			}
		}
		return result;
	}

	private void Change(string themeName)
	{
		foreach (BMThemeBase bmthemeBase in this.themeItems)
		{
			bmthemeBase.Change(themeName);
		}
	}

	private const string themeSettingsAssetName = "BMThemeSettings";

	private const string themeSettingsPath = "Resources";

	private const string themeSettingsAssetExtension = ".asset";

	private static BMTheme instance;

	private string currentThemeName;

	private List<BMThemeBase> themeItems = new List<BMThemeBase>();

	public ThemeInfo[] themeInfos;
}
